﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using REVISIONMAX30.DataObjects;

namespace REVISIONMAX30.Controls
{
    public partial class MySourceDirectoryList : UserControl
    {
        public MySourceDirectoryList()
        {
            InitializeComponent();
        }


        public UIHandler uiHandler { get; set; }
        public DataObjects.Group group { get; set; }

        public void Initzialize(UIHandler pUIHandler, DataObjects.Group pParentObject)
        {
            this.uiHandler = pUIHandler;
            this.group = pParentObject;
            this.UpdateViewer();
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            var tSelectedGuidList = this.lvSources.SelectedItems.Cast<ListViewItem>().Select(t => t.Tag).Cast<Guid>().ToList();
            this.group.SourceDirectoryList.RemoveAll(t => tSelectedGuidList.Contains(t.DataObjectGUID));

            this.UpdateViewer();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            //this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            //if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    using (SQLiteConnection connection = Common.GetSQLiteConnection())
            //    {
            //        connection.Open();
            //        this._uiHandler.ViewSource(new Source(this.folderBrowserDialog1.SelectedPath, this._parent, connection));
            //    }
            //}
            //this.UpdateViewer();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.UpdateViewer();
        }


        private void Edit()
        {

            if (this.lvSources.SelectedItems.Count > 0)
            {
                var tSelectedGUID = (Guid)this.lvSources.SelectedItems[0].Tag;
                this.uiHandler.ViewSourceDirectory(this.group.SourceDirectoryList.Where(t => t.DataObjectGUID == tSelectedGUID).First(), this.group);
                this.UpdateViewer();
            }
        }


        private void lvSources_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }

        public void UpdateViewer()
        {
            this.lvSources.Items.Clear();

            foreach (DataObjects.SourceDirectory tSourceDirectory in this.group.SourceDirectoryList)
            {
                var tlvItem = this.lvSources.Items.Add(tSourceDirectory.DirectoryName);
                tlvItem.Tag = tSourceDirectory.DataObjectGUID;
                tlvItem.SubItems.Add(tSourceDirectory.ScanDirectoryRecusivly.BoolValueYesNo());
                tlvItem.SubItems.Add(tSourceDirectory.Created.StringValueOrEmpty());
                tlvItem.SubItems.Add(tSourceDirectory.Updated.StringValueOrEmpty());

                if (tSourceDirectory.DirectoryExists())
                {
                    tlvItem.BackColor = Color.LightSalmon;
                }

            }
        }

    }
}
